#include <stdio.h>
#include <stdlib.h>
#include "md5sum.h"
#include "structures.h"
#include "mod_utils.h"
#include <time.h>
#define FIRSTUP "firstUp.txt"
#define LASTUP "lastUp.txt"
#define TIMEUP "timeUp.txt"
#define LASTDOWN "lastDown.txt"
/*
 * Codice Modulo = 11
 */

char* hashFile(char *percorso){
    char digest[16];
    FILE *fp;

    if((fp = fopen(percorso,"r"))==NULL){
        printf("Errore in apertura del file in %s !",percorso);
        exit(1);
    }

    if(mdfile(fp, digest)<0){
        printf("Errore nel calcolo dell'Hash del file!");
        exit(1);
    }

    close(fp);

    char *hash2 = saveDigest(digest);
    char hash3[32];
    sprintf(hash3,"%s",hash2);

    return hash2;

}
int getLoad(){
    int load;
    
    load=1;
    return load;
}
void setLastUp(){
    printf("Sono dentro setLastUp\n");
    FILE * lastUp;
    time_t lu;
    lu = time(NULL);
    lastUp = fopen(LASTUP,"w");
    fwrite(&lu,sizeof(time_t),1,lastUp);
    fclose(lastUp);
}
time_t getLastUp(){
    FILE * lastUp;
    time_t lu;
    printf("Prima di lastUp (get last time)\n");
    lastUp = fopen(LASTUP,"r");
        printf("Dopo apertura file lasttime(get lasttime)\n");
    fread(&lu,sizeof(time_t),1,lastUp);
    fclose(lastUp);
    return lu;
}
time_t setFirstUp(){
    FILE *first;
    time_t ret;
        first = fopen(FIRSTUP,"r");
            //file non presente
          //  fclose(first);
            printf("File del primo ingresso non presente, creazione in corso\n");
            struct tm* myTime1;
            ret = time(NULL);
            first=fopen(FIRSTUP,"w");
            fwrite(&ret,sizeof(time_t),1,first);
            fclose(first);
            myTime1 = localtime(&ret);
            printf(asctime(myTime1));
            printf("Creo il file per timeUp\n");
            FILE * timeUp;
            timeUp = fopen(TIMEUP,"w");
            double z = 0.0;
            fprintf(timeUp,"%lf",&z);
            fclose(timeUp);
            printf("Creo il file lastUp");
            setLastUp();
            setLastDown();
            return ret;
}
time_t getFirstUp(){
    FILE *first;
    time_t ret;
        first = fopen(FIRSTUP,"r");
        if(first==NULL){
            ret=setFirstUp();
            return ret;
        }
        else{
            //file presente, si procede alla lettura
            printf("file presente, si procede alla lettura\n");
            fread(&ret,sizeof(time_t),1,first);
            struct tm* myTime2;
            myTime2=localtime(&ret);
            printf(asctime(myTime2));
            fclose(first);
            return ret;
        }
}
void setLastDown(){
    printf("Sono dentro setLastTIme\n");
    FILE * lastDown;
    time_t ld;
    ld = time(NULL);
    lastDown = fopen(LASTDOWN,"w");
    fwrite(&ld,sizeof(time_t),1,lastDown);
    fclose(lastDown);
    updateTimeUp();
}
time_t getLastDown(){
    FILE * lastDown;
    time_t ld;
    printf("Prima di lastDown (get last time)\n");
    lastDown = fopen(LASTDOWN,"r");
    printf("Dopo apertura file lasttime(get lasttime)\n");
    fread(&ld,sizeof(time_t),1,lastDown);
    fclose(lastDown);
    return ld;
}
void updateTimeUp(){
    printf("SOno dentro updateTimeUp\n");
    time_t lastUp;
    FILE * timeUp;
    timeUp=fopen(TIMEUP,"r");
    double temp;
    fscanf(timeUp,"%lf",&temp);
    fclose(timeUp);
    time_t lastTimeSigned = getLastDown();
    lastUp=getLastUp();
    double differenza = difftime(lastTimeSigned,lastUp);
    printf("Risultato differenza fra ora e lastTime: %lf\n",differenza);
    temp=temp+differenza;
    printf("Risultato nuovo timeUp: %lf\n",temp);
    timeUp=fopen(TIMEUP,"w");
    fprintf(timeUp,"%lf",temp);
    fclose(timeUp);
}
double getTimeUp(){
    FILE * timeUp;
    double tu;
    timeUp = fopen(TIMEUP,"r");
    fscanf(timeUp,"%lf",&tu);
    fclose(timeUp);
    return tu;
}
void getDisp(float * res){
    //leggo il primo ingresso
    time_t firstT = getFirstUp();
    time_t now = time(NULL);
    float timeUp = getTimeUp();
    float tempoTotale = difftime(now,firstT);
    printf("Dentro getAva, tempoTotale = %lf , TimeUp= %lf\n",tempoTotale,timeUp);
    if(timeUp==0 || tempoTotale==0){
        printf("TimeUp=0 oppure tempoTotale=0\n");
        (*res)=1.0;
    }
    else{
        printf("timeUp o tempoTotale DIVERSI da 0\n");
        (*res) =(float)(timeUp/tempoTotale);
    }
        
    printf("Disponibilita calcolata: %lf\n",(*res));
}
